import React, { useCallback, useRef } from "react";
import { useHistory } from "react-router-dom";
import style from "../style/permissionsnav.module.scss";
import { Input, Select, Button } from "antd";
import { search } from "../api/addpart";
const Permissionsearch = (props) => {
  const obj = useRef({});
  const history = useHistory();
  const onChange = useCallback((value) => {
    console.log(`selected ${value}`);
    value = value === "已启用" ? "1" : "0";
    obj.current.type = value;
  }, []);
  const inputvalue = useCallback((e) => {
    obj.current.name = e.target.value;
  }, []);
  const addAction = useCallback(
    (value) => {
      history.push(`${history.location.pathname}/${value}`);
    },
    [history]
  );
  const searchAction = useCallback(async () => {
    console.log(props.type);
    console.log(obj.current);
    if (props.type === "partment") {
      let result = await search(obj.current);
      console.log(result.data);
      props.tolist(result.data.list);
    }
  }, [props]);
  const resetAction = useCallback(() => {
    props.tolist(JSON.parse(localStorage.getItem("list")));
  }, [props]);
  return (
    <div className={style.search}>
      <Input
        placeholder={props.value}
        style={{ width: "50vh", margin: "20px 10px 0 25px" }}
        onChange={(e) => inputvalue(e)}
      />
      <Select
        placeholder="启用状态"
        onChange={onChange}
        style={{ margin: "20px 10px 0 0" }}
        options={[
          {
            value: "已启用",
            label: "已启用",
          },
          {
            value: "已停用",
            label: "已停用",
          },
        ]}
      />
      <Button
        style={{ margin: "20px 10px 0 0" }}
        onClick={() => searchAction()}
      >
        查询
      </Button>
      <Button style={{ margin: "20px 0 0 0" }} onClick={() => resetAction()}>
        重置
      </Button>
      <Button
        style={{ margin: "20px 0 0 10px" }}
        onClick={() => addAction(props.route)}
      >
        {props.text}
      </Button>
    </div>
  );
};

export default Permissionsearch;
